package com.meelive.ingkee.push.platform.vivo;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;

import androidx.annotation.Nullable;

import com.meelive.ingkee.push.InkePushHelper;
import com.meelive.ingkee.push.common.PushUtils;
import com.meelive.ingkee.push.model.Constance;
import com.meelive.ingkee.push.model.InkePushType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * author: fanfeiyue
 * time: 2021/11/10
 * des:
 */
public class VIVOPushDispatcher extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
            Log.i(Constance.TAG, "onCreate fixOrientation when Oreo, result = " + result);
        }
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        final Intent intent = getIntent();

        //获取通知消息的messagId
        long messageId = intent.getLongExtra("vivo_push_messageId", 0L);
        final String data = intent.getStringExtra("data");
        if (PushUtils.DEBUG) {
            Log.d(Constance.TAG, "VIVOPushDispatcher onNotificationMessageClicked is called. " + data);
        }
        if (TextUtils.isEmpty(data)) {
            onFinish();
            return;
        }
        InkePushHelper.getInstance().onNotificationClick(this, data, InkePushType.VIVOPUSH);
        onFinish();
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            Log.i(Constance.TAG, "avoid calling setRequestedOrientation when Oreo.");
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    public void onFinish() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
    }

    // 解决8.0崩溃问题
    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
